package com.simple.datax.services;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.simple.datax.bo.IDataxDictionaryBO;
import com.simple.datax.bo.IDataxReceiveChannelBO;
import com.simple.datax.bo.IDataxSendChannelBO;
import com.simple.datax.vo.DataxReceiveChannelVO;
import com.simple.datax.vo.DataxSendChannelVO;
import com.simple.util.common.Constants;
import com.simple.util.common.Utils;
import com.simple.util.object.PageInfoBean;
import com.simple.util.object.datax.DataxChannelBean;

@Controller 
@RequestMapping ("/services/channel") 
public class ChannelService extends AbstractDataxBaseService {
	@Autowired
	private IDataxDictionaryBO dataxDictionaryBO;
	
	@Autowired
	private IDataxReceiveChannelBO dataxReceiveChannelBO;
	
	@Autowired
	private IDataxSendChannelBO dataxSendChannelBO;

	@RequestMapping (value="/getReceiveChannel", headers="Accept=application/json") 
	@ResponseBody    
    public DataxChannelBean getReceiveChannel(
    		   @RequestParam(value="fiId",required=false) final String fiId,
			   final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException  {
		Map<String, String> optionCommunTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_TRANSFERSWAY);
        
        Map<String, String> optionMsgTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGETYPE);
        
        Map<String, String> optionMsgFormatMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGEFORMAT);
        
        Map<String, String> optionMsgCharsetMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGECHARACTER);
        
        DataxReceiveChannelVO dataxReceiveChannelVO = dataxReceiveChannelBO.findById(Utils.parseInt(fiId));
        
        DataxChannelBean dataxChannelBean = null;
    	if(dataxReceiveChannelVO!=null){
    		dataxChannelBean = new DataxChannelBean();
    		dataxChannelBean.setCommunParam(dataxReceiveChannelVO.getFcCommunParam());
    		dataxChannelBean.setCommunType(optionCommunTypeMap.get(dataxReceiveChannelVO.getFcCommunType()));
    		dataxChannelBean.setMsgFormate(optionMsgFormatMap.get(dataxReceiveChannelVO.getFcMsgFormateId()));
    		dataxChannelBean.setMsgCharset(optionMsgCharsetMap.get(dataxReceiveChannelVO.getFcMsgCharset()));
    		dataxChannelBean.setMsgType(optionMsgTypeMap.get(dataxReceiveChannelVO.getFcMsgType()));
    	}		

        return dataxChannelBean;
    }
	
	@RequestMapping (value="/getSendChannel", headers="Accept=application/json") 
	@ResponseBody    
    public DataxChannelBean getSendChannel(
    		   @RequestParam(value="fiId",required=false) final String fiId,
			   final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException  {
		Map<String, String> optionCommunTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_TRANSFERSWAY);
        
        Map<String, String> optionMsgTypeMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGETYPE);
        
        Map<String, String> optionMsgFormatMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGEFORMAT);
        
        Map<String, String> optionMsgCharsetMap = dataxDictionaryBO
				.findMapByCatagory(Constants.TRANS_PROPERTY_FICATAGORY_MESSAGECHARACTER);
        
        DataxSendChannelVO dataxSendChannelVO = dataxSendChannelBO.findById(Utils.parseInt(fiId));
        
        DataxChannelBean dataxChannelBean = null;
    	if(dataxSendChannelVO!=null){
    		dataxChannelBean = new DataxChannelBean();
    		dataxChannelBean.setCommunParam(dataxSendChannelVO.getFcCommunParam());
    		dataxChannelBean.setCommunType(optionCommunTypeMap.get(dataxSendChannelVO.getFcCommunType()));
    		dataxChannelBean.setMsgFormate(optionMsgFormatMap.get(dataxSendChannelVO.getFcMsgFormateId()));
    		dataxChannelBean.setMsgCharset(optionMsgCharsetMap.get(dataxSendChannelVO.getFcMsgCharset()));
    		dataxChannelBean.setMsgType(optionMsgTypeMap.get(dataxSendChannelVO.getFcMsgType()));
    	}		
		

        return dataxChannelBean;
    }
	@RequestMapping (value="/getMapSendChannel", headers="Accept=application/json") 
	@ResponseBody 
	public  List<Map<String,String>> getMapSendChannel(@RequestParam(value="term",required=false)  String term,
	   final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException  {
		//String term = request.getParameter("term");
		//term = new String(term.getBytes("ISO-8859-1"),"UTF-8");
		List<Map<String,String>> listData=new ArrayList<Map<String,String>>();
		List<DataxSendChannelVO> listSendChannelBO = dataxSendChannelBO.getDataxSendChannelVOByPage(new PageInfoBean(1), term);
	
		for(int i=0;i<listSendChannelBO.size();i++){
			Map<String, String> hashMap=new HashMap<String, String>();
			hashMap.put("value", listSendChannelBO.get(i).getFiId().toString());
			hashMap.put("label", listSendChannelBO.get(i).getFcChannelDesc());
			listData.add(hashMap);
		}
		return listData;
	}
	@RequestMapping (value="/getMapReceiveChannel", headers="Accept=application/json") 
	@ResponseBody 
	public   List<Map<String,String>>  getMapReceiveChannel(@RequestParam(value="term",required=false)  String term,
			final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException  {
		List<Map<String,String>> listData=new ArrayList<Map<String,String>>();
		List<DataxReceiveChannelVO> listReceiveChannelBO = dataxReceiveChannelBO.findReceiveChannelVOByPage(new PageInfoBean(1), term);
		for(int i=0;i<listReceiveChannelBO.size();i++){
			Map<String, String> hashMap=new HashMap<String, String>();
			hashMap.put("value", listReceiveChannelBO.get(i).getFiId().toString());
			hashMap.put("label", listReceiveChannelBO.get(i).getFcChannelDesc());
			listData.add(hashMap);
		}
		return listData;
	}
}
